var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
var app = getApp();

Page({
    data: {
        info:[],
        category:[],
        pageSize:20,
        pageNo:1,
        hasMore:true,
        categoryId:null
    },
    onPullDownRefresh: function () {
        this.getData(this.data.lat,this.data.lon)
    },
    onReachBottom: function(){
        let that = this
        this.loadMore(that.data.lat,that.data.lon)
    },
    onLoad: function(options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.setData({
            categoryId:options.id,
            lat:options.lat,
            lon:options.lon
          })
        this.getData(options.lat,options.lon)
        wx.setNavigationBarTitle({
          title: options.sort
        })
    },
    onCall(e){
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.tel,
        })
    },
    getData: function(lat,lon){
        let that = this;
        wx.showLoading({
          title: '加载中...',
        })
        that.setData({
            pageNo: 1,
            hasMore:true
        });
        util.request(api.surroundings.getIndexMerchant+'?pageSize='+that.data.pageSize+'&pageNo='+that.data.pageNo+'&lat='+lat+'&lon='+lon+'&categoryId='+that.data.categoryId, {},'get').then(res=>{
            that.setData({
                info:res.result
            })
            if (res.result.length < that.data.pageSize) {
                that.setData({
                    hasMore: false,
                })
            }
            wx.stopPullDownRefresh();
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
        }).finally(()=> wx.stopPullDownRefresh() )
        
    },
    loadMore: function(lat,lon) {
        var that = this;
        if (!this.data.hasMore) return
        wx.showLoading({
            title: '加载中...',
        })
        util.request(api.surroundings.getIndexMerchant+'?pageSize='+that.data.pageSize+'&pageNo='+(++that.data.pageNo)+'&lat='+lat+'&lon='+lon+'&categoryId='+that.data.categoryId, {},'get').then(res=>{
                that.setData({
                    info: that.data.info.concat(res.result)
                });
                if (res.result.length < that.data.pageSize) {
                    that.setData({
                        hasMore: false,
                    })
                }
                wx.hideLoading()
        }).catch(err=>{
            console.log(err)
        })
    },
    onReady: function() {

    },
    onShow: function() {
    
        
    },
    onHide: function() {
        // 页面隐藏

    },
    onUnload: function() {
        // 页面关闭
    }
    
})
